package com.exartisan.utils;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import com.exartisan.bean.Answer;
import com.exartisan.bean.Data;
import com.exartisan.bean.Quiz;
import com.exartisan.bean.Survey;


public class ResponseParser {
	public final static String HANDLE_UPDATE_INFO = "update_info";
	public final static String HANDLE_SAVE_DATA = "save_data";
	public final static String HANDLE_LOAD_SAVED_DATA = "load_saved_data";
	public final static String HANDLE_SUBMIT_CONTEST = "submit_contest";
	public final static String HANDLE_REGISTER = "on_register";
	public final static String HANDLE_LOGIN = "on_login";
	public final static String HANDLE_LOAD_SURVEY = "on_load_survey";
	public final static String HANDLE_LOAD_QUESTION = "on_load_question";
	public final static String HANDLE_ON_SYSTEM_ERROR = "on_system_error";
	public final static String HANDLE = "handle";
	public final static String SUCCESS = "success";
	public final static String MESSAGE = "message";
	public final static String ERROR_CODE = "error_code";
	public final static String DATA = "data";
	public final static String QUESTION_ID = "QuestionID";
	public final static String ANSWER = "answer";
	public final static String TIME = "time";
	public final static String QUESTION1 = "questions";
	public final static String QUESTION2 = "Question";
	public final static String ANSWER_TYPE = "AnswerType";
	public final static String LIST_ANSWER = "ListAnswer";
	public final static String CONTEST_NAME = "CategoryName";
	public final static String CREATE_DATE = "CreateDate";
	public final static String START_DATE = "CreateStart";
	public final static String END_DATE = "CreateEnd";
	public final static String STATUS = "Status";
	public final static String CONTEST_ID = "ID";
	public final static String USER_ID = "user_id";
	public final static String USER_ID_RECEIVE = "ID";
	public final static String USER_INFOR = "user_infor";
	public final static String TABLE = "Table";
	public final static String ID = "ID";
	public final static String ANSWER_CONTENT = "Content";
	public final static String TRUE_ANSWER = "TrueAnswer";
	public final static String ANSWER_ID = "ID";
	public final static String ANSWER_TEXT = "AnswerText";
	
	
	public static boolean checkSuccess(String jsonResponse){
		JSONObject jsonObject = new JSONObject(jsonResponse);
		if(jsonObject.getString(HANDLE).equals(HANDLE_ON_SYSTEM_ERROR)) return false;
		else return true;
	}
	
	public static String getHandle(String jsonResponse){
		JSONObject jsonObject = new JSONObject(jsonResponse);
		return jsonObject.getString(HANDLE);
	}
	
	public static String getErrorCode(String jsonResponse){
		JSONObject jsonObject = new JSONObject(jsonResponse);
		return jsonObject.getString(ERROR_CODE);
	}
	
	public static String getErrorMessage(String jsonResponse){
		JSONObject jsonObject = new JSONObject(jsonResponse);
		return jsonObject.getString(MESSAGE);
	}
	
	public static String getUserID(String jsonResponse){
		JSONObject jsonObject = new JSONObject(jsonResponse);
		if(getHandle(jsonResponse).equals(HANDLE_LOGIN) || getHandle(jsonResponse).equals(HANDLE_REGISTER)){
			return new JSONObject(jsonObject.getString(USER_INFOR)).getString(USER_ID_RECEIVE);
		} else return "FOOBAR";
	}
	
	public static String getAreaName(String jsonResponse){
		JSONObject jsonObject = new JSONObject(jsonResponse);
		if(getHandle(jsonResponse).equals(HANDLE_LOGIN) || getHandle(jsonResponse).equals(HANDLE_REGISTER)){
			return new JSONObject(jsonObject.getString(USER_INFOR)).getString("AreaName");
		} else return "FOOBAR";
	}
	
	public static ArrayList<Quiz> getQuestionData(String jsonResponse){
		ArrayList<Quiz> listQuestion = new ArrayList<Quiz>(); 
		long questionID;
		String question;
		long type;
		ArrayList<Answer> answers;
		JSONObject jsonrp = new JSONObject(jsonResponse);
		JSONObject jsonQuestion = jsonrp.getJSONObject(QUESTION1);
		JSONArray jsonTable = jsonQuestion.getJSONArray(TABLE);
		for (int i = 0; i < jsonTable.length(); i++) {
			JSONObject jsonRecord = jsonTable.getJSONObject(i);
			questionID = jsonRecord.getLong(QUESTION_ID);
			question = jsonRecord.getString(QUESTION2);
			type = jsonRecord.getLong(ANSWER_TYPE);
			JSONArray jsonAnswers = jsonRecord.getJSONArray(LIST_ANSWER);
			answers = new ArrayList<Answer>();
			for (int j = 0; j < jsonAnswers.length(); j++) {
				JSONObject jsonObject = jsonAnswers.getJSONObject(j);
				Answer aAnswer = new Answer(jsonObject.getLong(ANSWER_ID), jsonObject.getString(ANSWER_CONTENT));
				answers.add(aAnswer);
			}
			Quiz aQuiz = new Quiz(questionID, question, type, answers);
			listQuestion.add(aQuiz);
		}
		return listQuestion;
	}
	
	public static ArrayList<Survey> getContestData(String jsonResponse){
		ArrayList<Survey> listSurvey = new ArrayList<Survey>();
		long surveyID;
		String surveyName;
		String CreateDate;
		String StartDate;
		String EndDate;
		int status;
		JSONObject jsonObject = new JSONObject(jsonResponse);
		JSONObject jsonObject2 = jsonObject.getJSONObject(QUESTION1);
		JSONArray jsonrp = jsonObject2.getJSONArray(TABLE);
		for (int i = 0; i < jsonrp.length(); i++) {
			JSONObject jsonSurvey = jsonrp.getJSONObject(i);
			surveyID = jsonSurvey.getLong(CONTEST_ID);
			surveyName = jsonSurvey.getString(CONTEST_NAME);
			CreateDate =jsonSurvey.getString(CREATE_DATE);
			StartDate = jsonSurvey.getString(START_DATE);
			EndDate = jsonSurvey.getString(END_DATE);
			status = jsonSurvey.getInt(STATUS);
			Survey aSurvey = new Survey(surveyID, surveyName, CreateDate, StartDate, EndDate, status);
			listSurvey.add(aSurvey);
		}
		return listSurvey;
	}
	//
	
	public static ArrayList<Data> getUserData(String jsonResponse){
		ArrayList<Data> data = new ArrayList<Data>();
		long questionID;
		String userAnswer;
		long answerID;
		JSONObject jsonrp = new JSONObject(jsonResponse);
		JSONObject jsonData = jsonrp.getJSONObject(DATA);
		JSONArray jsonTable = jsonData.getJSONArray(TABLE);
		for (int i = 0; i < jsonTable.length(); i++) {
			JSONObject jsonRecord = jsonTable.getJSONObject(i);
			questionID = jsonRecord.getLong(QUESTION_ID);
			userAnswer = jsonRecord.getString(ANSWER_TEXT);
			answerID = jsonRecord.getLong(ANSWER_ID);
			Data aQuizData = new Data(questionID, userAnswer, answerID);
			data.add(aQuizData);
		}
		return data;
	}
	
}
